var mycart = new Cart();
function addToCart(fid, fname, fprice) {
	
	var offset = $("#end").offset(); //结束的地方的元素
	var addcar = $(this);
	var flyer = $(' <button type="button" class="btn add-btn" style="width: 80px;height: 50px;"><i class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></i></button>');
	flyer.fly({
		start : {
			left : event.pageX + 10,
			top : event.pageY
		},
		end : {
			left : offset.left + 10,
			top : offset.top + 10,
			width : 0,
			height : 0
		},
		onEnd : function() {
			addcar.css("cursor", "default").removeClass('orange').unbind(
					'click');
			this.destory();
		}
	});
	
	var food = new Food(fid, fname, parseFloat(fprice));
	mycart.addFood(food);
	$.ajax({
		url : "action/addToCart",
		type : "get",
		data : {
			fId : fid,
			type : 1
		},
		success : function(result) {
			console.log(result);
			if(result == "Please login first!")
			{
				alert("请先登录");
				 $("#SignInModal").modal("show");
				mycart = new Cart();
			}
			showFoodCart();
		}
	})
}

function setFoodNum(id, num) {
	if (num > 100) {
		alert("请勿恶意输入");
		return 0;
	}
	var food = new Food(id, "A", 11, num);
	mycart.setFood(food);
	$.ajax({
		url : "action/addToCart",
		type : "get",
		data : {
			fId : id,
			type : 2,
			num : num
		},
		success : function(result) {
			console.log(result);
			if(result == "Please login first!")
			{
				alert("请先登录");
				 $("#SignInModal").modal("show");
				mycart = new Cart();
			}
			showFoodCart();
		}
	})
}

function showFoodCart() {
	$("#cartBody").empty();
	var cartBody = $("#cartBody").get(0);
	var foodCart = mycart.foodCart
	for (foodid in foodCart) {
		var tr = $("<tr></tr>");
		tr.appendTo(cartBody);
		var td1 = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");
		td1.innerHTML = foodCart[foodid].fname;
		var input = document.createElement('input');
		input.setAttribute("type", "number");
		input.setAttribute("value", foodCart[foodid].num);
		input.setAttribute("fid", foodCart[foodid].fid);
		input.onchange = function() {
			setFoodNum(foodid, this.value);
		}
		console.log(input);
		td2.appendChild(input);
		td3.innerHTML = foodCart[foodid].price;
		tr.append(td1);
		tr.append(td2);
		tr.append(td3);
	}
	$("#total").get(0).innerHTML = mycart.sum;
}

function loadCart(foods) {
	console.log(foods);
	for (arr in foods) {
		var thisfood = foods[arr];
		var loadfood = new Food(thisfood.fId, thisfood.fName, thisfood.fPrice,
				thisfood.num);
		mycart.addFood(loadfood);
		loadfood.price = accMul(loadfood.price, loadfood.num);
		mycart.setFood(loadfood);
	}

}

function commitOrder() {
	var getmid = $("#midinput").get(0).value;
	$.ajax({
		url : "action/addOrder",
		type : "post",
		data : {
			mId : getmid
		},
		success : function(result) {
			console.log(result);
			if(!result){
				alert("商家已暂停营业!!!");
			}
			mycart = new Cart();
			showFoodCart();
		}
	})
}

function showFoodOrder() {
	$("#cart-orderbody").empty();
	var cartBody = $("#cart-orderbody").get(0);
	var foodCart = mycart.foodCart
	for (foodid in foodCart) {
		var tr = $("<tr></tr>");
		tr.appendTo(cartBody);
		var td1 = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");
		td1.innerHTML = foodCart[foodid].fname;
		td2.innerHTML = foodCart[foodid].num;
		td3.innerHTML = foodCart[foodid].price;
		tr.append(td1);
		tr.append(td2);
		tr.append(td3);
	}
	$("#order-total").get(0).innerHTML = mycart.sum;
	$.ajax({
		url : "action/getUserLocation",
		type : "get",

		success : function(result) {
			console.log(result);
			$("#userLocation").get(0).innerHTML = "送货地址："+result.cLocation;
		}
	})
}

